package com.ym.jiujiangeyou;

import com.ym.jiujiangeyou.app.AppManager;

import com.ym.jiujiangeyou.app.MyApplication;

import com.ym.jiujiangeyou.fragment.CollectFragment;
import com.ym.jiujiangeyou.fragment.IndexFragment;
import com.ym.jiujiangeyou.fragment.MineFragment;
import com.ym.jiujiangeyou.fragment.MoreFragment;
import com.ym.jiujiangeyou.fragment.WeatherFragment;
import com.ym.jiujiangeyou.util.DummyTabContent;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
public class MainActivity extends FragmentActivity {

	TabHost tabHost;
	TabWidget tabWidget;
	LinearLayout bottom_layout;
	int CURRENT_TAB = 0; // 设置常量
	CollectFragment collectFrament;
	// Order_Frament order_Frament ;
	WeatherFragment weatherFragment;
	MineFragment mineFragment;
	MoreFragment moreFragment;
	IndexFragment indexFrament;
	android.support.v4.app.FragmentTransaction ft;
	LinearLayout tabIndicator1, tabIndicator2, tabIndicator3, tabIndicator4,
			tabIndicator5;
	// long mExitTime = System.currentTimeMillis();
	private MyApplication application;
	private long waitTime = 2000;
	private long touchTime = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		super.setContentView(R.layout.activity_main);

		findTabView();
		tabHost.setup();

		/** 监听 */
		TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {

				/** 碎片管理 */
				android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
				mineFragment = (MineFragment) fm.findFragmentByTag("mine");
				// order_Frament = (Order_Frament) fm.findFragmentByTag("order")
				// ;
				weatherFragment = (WeatherFragment) fm
						.findFragmentByTag("weather");
				collectFrament = (CollectFragment) fm
						.findFragmentByTag("collect");
				moreFragment = (MoreFragment) fm.findFragmentByTag("more");
				indexFrament = (IndexFragment) fm.findFragmentByTag("index");
				ft = fm.beginTransaction();

				/** 如果存在Detaches掉 */
				if (mineFragment != null)
					ft.detach(mineFragment);

				/** 如果存在Detaches掉 */
				if (weatherFragment != null)
					ft.detach(weatherFragment);

				/** 如果存在Detaches掉 */
				if (collectFrament != null)
					ft.detach(collectFrament);

				/** 如果存在Detaches掉 */
				if (moreFragment != null)
					ft.detach(moreFragment);

				if (indexFrament != null)
					ft.detach(indexFrament);

				/** 如果当前选项卡是home */
				if (tabId.equalsIgnoreCase("collect")) {
					isTabCollect();
					CURRENT_TAB = 1;

					/** 如果当前选项卡是wall */
				} else if (tabId.equalsIgnoreCase("weather")) {
					// isTabOrder() ;
					isTabWeather();
					CURRENT_TAB = 2;

					/** 如果当前选项卡是message */
				} else if (tabId.equalsIgnoreCase("index")) {
					isTabIndex();
					CURRENT_TAB = 5;
				} else if (tabId.equalsIgnoreCase("mine")) {
					isTabMine();
					CURRENT_TAB = 3;

					/** 如果当前选项卡是me */
				} else if (tabId.equalsIgnoreCase("more")) {
					isTabMore();
					CURRENT_TAB = 4;
				} else {
					switch (CURRENT_TAB) {
					case 1:
						isTabCollect();
						break;
					case 2:
						// isTabOrder() ;
						isTabWeather();
						break;
					case 3:
						isTabMine();
						// Toast.makeText(getApplication(), "ff", 1).show() ;
						break;
					case 4:
						isTabMore();
						break;
					case 5:
						isTabIndex();
						break;
					default:
						// isTabCollect() ;
						isTabIndex();
						break;
					}

				}
				ft.commit();
			}

		};
		// 设置初始选项卡
		tabHost.setCurrentTab(2);
		tabHost.setOnTabChangedListener(tabChangeListener);
		initTab();
		/** 设置初始化界面 */
		tabHost.setCurrentTab(2);

	}

	// 判断当前
	public void isTabCollect() {

		if (collectFrament == null) {
			ft.add(R.id.realtabcontent, new CollectFragment(), "collect");
		} else {
			ft.attach(collectFrament);
		}
	}

	public void isTabMine() {

		if (mineFragment == null) {
			ft.add(R.id.realtabcontent, new MineFragment(), "mine");
		} else {
			ft.attach(mineFragment);
		}

	}

	/*
	 * public void isTabOrder(){
	 * 
	 * if(order_Frament==null){ ft.add(R.id.realtabcontent,new Order_Frament(),
	 * "order"); }else{ ft.attach(order_Frament); } }
	 */
	public void isTabWeather() {

		if (weatherFragment == null) {
			ft.add(R.id.realtabcontent, new WeatherFragment(), "weather");
		} else {
			ft.attach(weatherFragment);
		}
	}

	public void isTabMore() {

		if (moreFragment == null) {
			ft.add(R.id.realtabcontent, new MoreFragment(), "more");
		} else {
			ft.attach(moreFragment);
		}
	}

	public void isTabIndex() {
		if (indexFrament == null) {
			ft.add(R.id.realtabcontent, new IndexFragment(), "index");
		} else {
			ft.attach(indexFrament);
		}
	}

	/**
	 * 找到Tabhost布局
	 */
	public void findTabView() {

		tabHost = (TabHost) findViewById(android.R.id.tabhost);
		tabWidget = (TabWidget) findViewById(android.R.id.tabs);
		LinearLayout layout = (LinearLayout) tabHost.getChildAt(0);
		TabWidget tw = (TabWidget) layout.getChildAt(1);

		tabIndicator1 = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab1 = (TextView) tabIndicator1.getChildAt(1);
		ImageView ivTab1 = (ImageView) tabIndicator1.getChildAt(0);
		ivTab1.setBackgroundResource(R.drawable.selector_tab_collect); // shou
																		// cang
		tvTab1.setText(R.string.tab_collect);

		tabIndicator2 = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab2 = (TextView) tabIndicator2.getChildAt(1);
		ImageView ivTab2 = (ImageView) tabIndicator2.getChildAt(0);
		ivTab2.setBackgroundResource(R.drawable.selector_tab_weather); // 天气
		tvTab2.setText(R.string.tab_tianqi);

		tabIndicator3 = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab3 = (TextView) tabIndicator3.getChildAt(1);
		ImageView ivTab3 = (ImageView) tabIndicator3.getChildAt(0);
		ivTab3.setBackgroundResource(R.drawable.selector_tab_index); // 首页
		tvTab3.setText(R.string.tab_index);

		tabIndicator4 = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab4 = (TextView) tabIndicator4.getChildAt(1);
		ImageView ivTab4 = (ImageView) tabIndicator4.getChildAt(0);
		ivTab4.setBackgroundResource(R.drawable.selector_tab_location); // location
		tvTab4.setText(R.string.tab_location);

		tabIndicator5 = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab5 = (TextView) tabIndicator5.getChildAt(1);
		ImageView ivTab5 = (ImageView) tabIndicator5.getChildAt(0);
		ivTab5.setBackgroundResource(R.drawable.selector_tab_setting); // 设置
		tvTab5.setText(R.string.tab_more);
	}

	/**
	 * 初始化选项卡
	 * 
	 * */
	public void initTab() {

		TabHost.TabSpec tSpecCollect = tabHost.newTabSpec("collect");
		tSpecCollect.setIndicator(tabIndicator1);
		tSpecCollect.setContent(new DummyTabContent(getBaseContext()));
		tabHost.addTab(tSpecCollect);

		TabHost.TabSpec tSpecOrder = tabHost.newTabSpec("weather");
		tSpecOrder.setIndicator(tabIndicator2);
		tSpecOrder.setContent(new DummyTabContent(getBaseContext()));
		tabHost.addTab(tSpecOrder);

		TabHost.TabSpec tSpecIndex = tabHost.newTabSpec("index");
		tSpecIndex.setIndicator(tabIndicator3);
		tSpecIndex.setContent(new DummyTabContent(getBaseContext()));
		tabHost.addTab(tSpecIndex);

		TabHost.TabSpec tSpecMessage = tabHost.newTabSpec("mine");
		tSpecMessage.setIndicator(tabIndicator4);
		tSpecMessage.setContent(new DummyTabContent(getBaseContext()));
		tabHost.addTab(tSpecMessage);

		TabHost.TabSpec tSpecMore = tabHost.newTabSpec("more");
		tSpecMore.setIndicator(tabIndicator5);
		tSpecMore.setContent(new DummyTabContent(getBaseContext()));
		tabHost.addTab(tSpecMore);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * 退出系统
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& KeyEvent.KEYCODE_BACK == keyCode) {
			long currentTime = System.currentTimeMillis();
			if ((currentTime - touchTime) >= waitTime) {
				Toast.makeText(this, R.string.exit_down_again, Toast.LENGTH_SHORT).show();
				touchTime = currentTime;
			} else {
				// 退出
				AppManager.getAppManager().AppExit(getApplicationContext());
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
